import Component from './component'
import {
    wrapToVdom
} from "./utils";
import {
    REACT_ELEMENT,
    REACT_FORWARD_REF_TYPE,
    REACT_CONTEXT, 
    REACT_PROVIDER
} from "./constants";

function createElement(type, config, children) {
    let key, ref
    if (config) {
        key = config.key //引用真实的dom元素
        ref = config.ref //
        delete config.ref
        delete config.key
    }
    let props = {
        ...config
    }
    if (arguments.length > 3) {
        props.children = Array.prototype.slice.call(arguments, 2).map(wrapToVdom)
    } else if (arguments.length === 3) {
        props.children = wrapToVdom(children);
    }
    return { //这就是vdom
        $$typeof: REACT_ELEMENT, //元素的类型
        type, //标签类型
        ref,
        key,
        props, //classname style  children
    }
}

function createRef(){
    return { current:null }
}
/**
 * 就是一个可以接受转发ref的函数组件
 * @param {*} render  
 */
function forwardRef(render){
    return {
        $$typeof: REACT_FORWARD_REF_TYPE,
        render:render
    }
}
function  createContext(params) {
    // debugger
    let context = {
        $$typeof:REACT_CONTEXT,
    }
    context.Provider = {
        $$typeof: REACT_PROVIDER,
        _context: context
    }
    context.Consumer = {
        $$typeof: REACT_CONTEXT,
        _context: context
    }
    return context
    
}

const React = {
    createElement,
    Component,
    createRef,
    forwardRef,
    createContext
}
export default React